Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fluent-bit-plugin: Auto add Kubernetes labels to Loki labels #1204

Merged
merged 6 commits into from
Nov 27, 2019

Conversation

allanhung
Copy link
Contributor

@allanhung allanhung commented Oct 27, 2019

What this PR does / why we need it:
It is too complex to change LabelMap every time when we deploy appliction in new labels. If you install 3rd add-on, you even don't know what the labels are. This PR provides a way to automatic parse the metadata when you set lineformat with "kubernetes".

Checklist

  • Documentation added
  • Tests updated

@CLAassistant
Copy link

CLAassistant commented Oct 27, 2019

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change, but I think this should be a root configuration not an extension of the line format that is used to format the actual line of a record not its label.

If you can refactor to use a new property a boolean for instance.
auto_kubernetes_labels boolean to automatically set kubernetes metadata on each records, may be ignore labelMap, LabelKeys, RemoveKeys.

Why wasn't the labelMap not working for you ?

@allanhung
Copy link
Contributor Author

@cyriltovena Thanks your advice. I had already refactored the code. The labelMap works for me but I have to get all the labels in our kubernetes application and set them into labelMap. I think that would be a tough task.

@@ -16,6 +16,7 @@ This plugin is implemented with [Fluent Bit's Go plugin](https://github.com/flue
| Labels | labels for API requests. | {job="fluent-bit"} |
| LogLevel | LogLevel for plugin logger. | "info" |
| RemoveKeys | Specify removing keys. | none |
| AutoKubernetesLabels | If set to ture, it will auto parse kubernetes metadata into labels | false |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| AutoKubernetesLabels | If set to ture, it will auto parse kubernetes metadata into labels | false |
| AutoKubernetesLabels | If set to true, it will add all kubernetes labels to loki labels | false |

kuberneteslbs := model.LabelSet{}
replacer := strings.NewReplacer("/", "_", ".", "_", "-", "_")
for k, v := range records["kubernetes"].(map[interface{}]interface{}) {
if k.(string) == "labels" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use a switch instead

switch key := k.(string) {
  case "labels":
....
  case  "docker_id", "pod_id","annotations":
    continue
  case default:
 ....
}

@@ -27,6 +28,10 @@ Labels are used to [query logs](../../docs/logql.md) `{container_name="nginx", c

You can use `Labels`, `RemoveKeys` , `LabelKeys` and `LabelMapPath` to how the output plugin will perform labels extraction.

### AutoKubernetesLabels

If set to ture, it will parser kubernetes metadata into labels automatically and ignore the paramater `LabelKeys`, LabelMapPath.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If set to ture, it will parser kubernetes metadata into labels automatically and ignore the paramater `LabelKeys`, LabelMapPath.
If set to true, it will add Kubernetes metadata into Loki labels automatically and ignore parameters `LabelKeys`, LabelMapPath.

@cyriltovena
Copy link
Contributor

I like this feature, good job @allanhung.

@allanhung allanhung changed the title fluent-bit-plugin: Auto parse kubernetes metadata into labels fluent-bit-plugin: Auto add Kubernetes labels to Loki labels Nov 6, 2019
@allanhung
Copy link
Contributor Author

@cyriltovena I had fixed the typo and use switch instead of if, I also run go fmt. Thanks for your suggestion.

@cyriltovena
Copy link
Contributor

if you have time to rebase to master let me know.

@allanhung
Copy link
Contributor Author

@cyriltovena Rebase has been done.

Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cyriltovena cyriltovena merged commit 30ec4eb into grafana:master Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants